package com.miaosha.ms_04.controller;

import com.miaosha.ms_04.domain.OrderInfo;
import com.miaosha.ms_04.domain.User;
import com.miaosha.ms_04.result.CodeMsg;
import com.miaosha.ms_04.service.GoodsService;
import com.miaosha.ms_04.service.MiaoshaService;
import com.miaosha.ms_04.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/19 16:09
 **/
@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {

    @Autowired
    private MiaoshaService miaoshaService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/do_miaosha")
    public String buy(Model model, User user, @RequestParam("goodsId") Long goodsId) {
        GoodsVo goods = goodsService.getGoodsById(goodsId);
        //库存校验
        Integer stockCount = goods.getStockCount();
        if (stockCount <= 0) {
            model.addAttribute("errmsg",CodeMsg.STOCK_NOT_ENOUGH);
            return "miaosha_fail";
        }

        OrderInfo orderInfo = miaoshaService.buyGoods(goods, user);
        model.addAttribute("goods", goods);
        model.addAttribute("orderInfo", orderInfo);
        return "/order_detail";
    }
}
